<template>
  <div class="ele-body">
    <!-- 搜索表单 -->

    <el-card shadow="never">
      <!-- 数据表格 -->
      <storedata-search @search="reload" />
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
      >
        <template v-slot:user="{ row }">
          <div class="ele-cell" v-if="row.userInfo">
            <el-avatar
              v-if="row.userInfo.avatar"
              :src="row.userInfo.avatar"
              shape="square"
              :size="40"
            />
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.userInfo.nickname }} </div>
              <div class="ele-cell-desc">{{ row.userInfo.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>
        <template v-slot:store_logo="{ row }">
          <el-avatar
            v-if="row.store_logo"
            :src="row.store_logo"
            shape="square"
            :size="40"
          />
        </template>
      <template v-slot:address="{ row }">
              <div class="ele-cell-title">{{ row.address }} {{row.detail}}</div>
        </template>
     
        <template v-slot:platformInfo="{ row }">
          <div class="ele-cell" v-if="row.platformInfo">
            <div class="ele-cell-content">
              <div class="ele-cell-title">{{ row.platformInfo.name }} </div>
              <div class="ele-cell-desc">{{ row.platformInfo.telphone }} </div>
            </div>
          </div>
          <span v-else>无</span>
        </template>

        <template v-slot:is_application="{ row }">
          <div class="ele-cell-content">
            <ele-dot
              v-if="row.is_application == 0"
              type="warning"
              effect="plain"
              text="未进件"
              :ripple="true"
            />
            <ele-dot
              v-if="row.is_application == 1"
              type="success"
              effect="plain"
              text="进件待审核"
              :ripple="true"
            />
            <ele-dot
              v-if="row.is_application == 2"
              type="success"
              effect="plain"
              text="进件成功"
              :ripple="true"
            />
             <ele-dot
              v-if="row.is_application ==3"
              type="success"
              effect="plain"
              text="驳回"
              :ripple="true"
            />
          </div>
        </template>

        <template v-slot:business_start_time="{ row }">
          <div class="ele-cell-content">
            <div class="ele-cell-title">{{ row.business_start_time }} </div>
          
          </div>
        </template>
        <template v-slot:status="{ row }">
          <ele-dot
            v-if="row.status == 0"
            type="warning"
            effect="plain"
            text="待审核"
            :ripple="true"
          />
          <ele-dot
            v-if="row.status == 1"
            type="success"
            effect="plain"
            text="审核通过"
            :ripple="true"
          />
          <ele-dot
            v-if="row.status == 2"
            type="success"
            effect="plain"
            text="驳回"
            :ripple="true"
          />
        </template>

        <template v-slot:action="{ row }">
         

           <el-popconfirm   v-if="row.status == 0 && platform_id >0"
            class="ele-action"
            title="审核通过"
            @confirm="editStatus(row,1)"
          >
            <template v-slot:reference>
              <el-link type="success" :underline="false" >
                审核通过
              </el-link>
            </template>
          </el-popconfirm>


           <el-popconfirm   v-if="row.status == 0 && platform_id >0"
            class="ele-action"
            title="驳回"
            @confirm="editStatus(row,2)"
          >
            <template v-slot:reference>
              <el-link type="warning" :underline="false" >
                驳回
              </el-link>
            </template>
          </el-popconfirm>

           <el-link v-if="row.is_application != 0 && platform_id >0"
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
          >
            进件数据
          </el-link>


          <el-popconfirm  v-if=" platform_id >0"
            class="ele-action"
            title="确定要删除此数据吗？"
            @confirm="remove(row)"
          >
            <template v-slot:reference>
              <el-link type="danger" :underline="false" icon="el-icon-delete">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
     <storedata-edit :visible.sync="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script>
import { list, remove, modify, sortChange } from '@/api/storedata';
import StoredataSearch from './components/storedata-search.vue';
import StoredataEdit from './components/storedata-edit.vue';
import Sortable from 'sortablejs';

export default {
  name: 'storedata',
  components: {
    StoredataSearch,
    StoredataEdit
  },
    created(){
      this.platform_id=localStorage.getItem('platform_id')
  
  },

  data() {
    return {
      // 表格列配置
      columns: [
            {
          prop: 'platformInfo',
          label: '平台详情',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'platformInfo',
          show: localStorage.getItem('platform_id') == 0 ? true: false ,

        },
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: 'left'
        },
        {
          prop: 'user',
          label: '用户信息',
          showOverflowTooltip: true,
          minWidth: 180,
          slot: 'user'
        },

          {
          prop: 'name',
          label: '店铺持有人',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'store_name',
          label: '店铺名称',
          showOverflowTooltip: true,
          minWidth: 110
        },

        
        {
          prop: 'telphone',
          label: '联系方式',
          showOverflowTooltip: true,
          minWidth: 110
        },
        {
          prop: 'store_logo',
          label: '店铺logo',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'store_logo'
        },

         {
          prop: 'address',
          label: '店铺地址',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'address'
        },
        
         {
          prop: 'balance',
          label: '可提现金额',
          showOverflowTooltip: true,
          minWidth: 110,
         
        },
      
           {
          prop: 'withdrawn',
          label: '已提现',
          showOverflowTooltip: true,
          minWidth: 110,
         
        },
           {
          prop: 'freeze',
          label: '待打款',
          showOverflowTooltip: true,
          minWidth: 110,
         
        },
    

        

        {
          prop: 'business_start_time',
          label: '营业时间',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'business_start_time'
        },

        

        {
          prop: 'status',
          label: '状态',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'status'
        },

        {
          prop: 'is_application',
          label: '是否进件',
          showOverflowTooltip: true,
          minWidth: 110,
          slot: 'is_application'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 350,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: 'right',
          hideInSetting: true,
          showOverflowTooltip: true
        }
      ],
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示分配回收员
      showCollector: false,
      //弹框名称
      id: 0
    };
  },
  methods: {
    rowDrop() {
      const tbody = document.querySelector('.el-table__body-wrapper tbody');
      const _this = this;
      Sortable.create(tbody, {
        forceFallback: true,
        dragClass: 'drag',
        delay: 100,
        animation: 1000,
        onEnd({ newIndex, oldIndex }) {
          //更换表格数据 后续拖动坐标才会正确
          let data = _this.$refs.table.getData();
          let nid = data[newIndex].id;
          let oid = data[oldIndex].id;
          const currRow = data.splice(oldIndex, 1)[0];
          data.splice(newIndex, 0, currRow);

          sortChange(data)
            .then((msg) => {})
            .catch((e) => {
              _this.$refs.table.reload();
            });
          //去数据库更换排序
        }
      });
    },
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return list({ ...where, ...order, page, limit });
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where: where });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      remove(row.id)
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },

    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的数据吗?', '提示', {
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          remove(this.selection.map((d) => d.id))
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },

    /* 更改状态 */
    editStatus(row,status) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'status', value: status })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
        
          this.$message.error(e.message);
        });
    },
    /* 更改状态 */
    editRecommend(row) {
      const loading = this.$loading({ lock: true });
      modify({ id: row.id, field: 'is_recommend', value: row.is_recommend })
        .then((msg) => {
          loading.close();
          this.$message.success(msg);
          this.reload();
        })
        .catch((e) => {
          loading.close();
          row.is_recommend = row.is_recommend == 1 ? 2 : 1;
          this.$message.error(e.message);
        });
    }
  },
  mounted() {
    this.rowDrop();
  }
};
</script>

<style>
.drag {
  background: #000 !important;
  background-image: linear-gradient(#333, #999) !important;
}
</style>
